//
//  SiViewController.m
//  UI_12UITableViewCellHomeworkFirst
//
//  Created by dllo on 16/6/21.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "SiViewController.h"
#import "Movie.h"
#import "SITableViewCell.h"
@interface SiViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,retain) NSMutableArray *movieArray;

@end

@implementation SiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.movieArray = [NSMutableArray array];
    self.navigationItem.title = @"电影";
    [self handleData];
    [self createView];
    [self config];
}

-(void)handleData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"movielist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    NSArray *array = [dictionary valueForKey:@"result"];
    for (NSDictionary *dic in array){
        Movie *movie = [[Movie alloc] init];
        [movie setValuesForKeysWithDictionary:dic];
        
        [self.movieArray addObject:movie];
        [movie release];
    }
}
-(void)createView{
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[SITableViewCell class] forCellReuseIdentifier:@"reuse"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight =  100.0;
        
        
    [_tableView release];
}
    
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.movieArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SITableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
    
    tableViewCell.movie = movie;
    return tableViewCell;
}

-(void)config{
    self.navigationItem.rightBarButtonItem = [self editButtonItem];
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            return UITableViewCellEditingStyleInsert;
            break;
            
        default:
            return UITableViewCellEditingStyleDelete;
            break;
    }
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (editingStyle) {
        case UITableViewCellEditingStyleDelete:
        {
            [self.movieArray removeObjectAtIndex:indexPath.row];
            [tableView reloadData];
        }
            break;
        case UITableViewCellEditingStyleInsert:
        {
            Movie *movie = [[Movie alloc] init];
            movie.movieName = @"Brave heart";
            [self.movieArray insertObject:movie atIndex:indexPath.row];
            [tableView reloadData];
        }
            break;
        default:
            break;
    }
}

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    Movie *movie = [self.movieArray objectAtIndex:sourceIndexPath.row];
    [movie retain];
    
    [self.movieArray removeObjectAtIndex:sourceIndexPath.row];
    
    [self.movieArray insertObject:movie atIndex:destinationIndexPath.row];
    
    [movie release];
    [tableView reloadData];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
